'''
Created on Jul 20, 2012

@author: LENOVO
'''
import string

class Decryptor(object):
    '''
    This class is used to receive a code and break it
    '''
    ABC = string.ascii_lowercase
    ABC_len = len(ABC)

    def __init__(self):
        self.decipheredText = ""
        self.cipherToPlain = {}
    
    def getLetterPlace(self,letter):
        lowercase_letter = letter.lower()
        return Decryptor.ABC.find(lowercase_letter)
    
    def createCipherToPlain(self,plain,cipher):
        codeShift = self.getLetterPlace(plain[0])-self.getLetterPlace(cipher[0])
        self.cipherToPlain = {}
        for letter in Decryptor.ABC:
            self.cipherToPlain[letter] = Decryptor.ABC[(self.getLetterPlace(letter)+codeShift) % Decryptor.ABC_len]
    
    def getPlainText(self,plain,cipher,to_crack):
        '''
        From the plain and cipher we learn the codeShift.
        toCrack is the cipher we want to break
        '''
        self.decipheredText = ""
        for cipherLetter in to_crack:
            decipheredLetter = self.cipherToPlain.get(cipherLetter)
            if decipheredLetter is None:
                self. decipheredText += cipherLetter
            else:
                self.decipheredText += decipheredLetter
        return self.decipheredText